home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 565 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.4 KB

  1. Path: solon.com!not-for-mail
  2. From: baynes@ukpsshp1.serigate.philips.nl
  3. Newsgroups: comp.lang.c.moderated,comp.std.c
  4. Subject: Re: 'h' modifier in printf
  5. Followup-To: comp.lang.c.moderated,comp.std.c
  6. Date: 14 Mar 1996 07:55:01 -0600
  7. Organization: Digital Solutions
  8. Sender: clc@solutions.solon.com
  9. Approved: clc@solutions.solon.com
  10. Message-ID: <4i98fl$8ml@solutions.solon.com>
  11. References: <4i801c$455@solutions.solon.com>
  12. NNTP-Posting-Host: solutions.solon.com
  13.  
  14. Michael J Zehr (tada@athena.mit.edu) wrote:
  15.  
  16. : I was recently asked a question about printf whose answer I couldn't
  17. : determine by reading K&R2 (and alas the company doesn't have a copy of
  18. : the standard to refer to).
  19.  
  20. >From the copy of the standard in Plauger's book:
  21.     "An optional h specifying that a following d, i, o, u, x or X conversion
  22.     specifier applies to a short int or unsigned short argument (the argument
  23.     will have been promoted according to the integral promotions, and its value
  24.     shall be converted to short in or unsigned short int before printing);..."
  25.  
  26. : The "h" modifier says the corresponding argument will be printed as a
  27. : short or unsigned short.
  28.  
  29. : So, given:
  30.  
  31. : short s;
  32. : printf("%d", s);
  33. : printf("%hd", s);
  34.  
  35. : (Assuming of course that s has been initialized at some point.)
  36.  
  37. : Can these two ever be different?  I'm aware of course that the short is
  38. : widened to an int during the function call, but this preserves the
  39. : value.
  40.  
  41. The should never be different.
  42.  
  43. : (A totally non-relevant piece of information is that on at least one
  44. : platform there is never a difference between these two for any value of
  45. : s from SHRT_MIN to SHRT_MAX.  But that doesn't answer the question.)
  46.  
  47. Good.
  48.  
  49. : This is the main question I'm interested, but as a followup, if these
  50. : always result in the same output, why is the 'h' modifier defined in the
  51. : first place?
  52.  
  53. Probably so you can use the same format string with scanf.
  54.  
  55. : Some speculations:
  56.  
  57. : int i;
  58. : printf("%hd", i);
  59. : printf("%d", (short)i);
  60.  
  61. : This question is stretching a bit to try to find what if anything the
  62. : 'h' modifier is ever used for.  Certainly the first line would have a
  63. : different result without the 'h', but casting seems like it ought to
  64. : have the same result.
  65.  
  66. I think if the value of i is outside SHRT_MIN to SHRT_MAX then both are
  67. undefined for different reasons. The first printf (%hd) because the conversion
  68. does not apply to a short integer. The second printf (%d of (short)) because
  69. the cast of i to (short) is not defined when i is out of range. If you had
  70. used unsigned numbers and the u conversion then the second would become
  71. defined because casting to a shorter unsigned number is defined, the first 
  72. would still be undefined. If i is in the range SHRT_MIN to SHRT_MAX you are
  73. probably OK. However one could argue that a perverse implementation might 
  74. have some hidden means of detecting that the integer passed to printf was
  75. actually taken from a short or an integer and then objecting in this case if
  76. it did not come from as short as the standard specifies.
  77.  
  78. I understand different real implementations will behave differently if
  79. the value passed to an 'h' modified conversion is out of range for the
  80. appropriate short type.
  81.  
  82. 'h' adds nothing useful over not using it, it just increases the potential
  83. for undefined behaviour.
  84.  
  85.  
  86. --
  87. Stephen Baynes                              baynes@ukpsshp1.serigate.philips.nl
  88. Philips Semiconductors Ltd
  89. Southampton                                 My views are my own.
  90. United Kingdom
  91.